package com.xoa.service.equipment.impl;

import com.alibaba.fastjson.JSONArray;
import com.xoa.dao.equipment.EquipTypeMapper;
import com.xoa.dao.equipment.LimsEquipEventlogMapper;
import com.xoa.dao.equipment.LimsEquipEventsCapyMapper;
import com.xoa.dao.equipment.LimsEquipEventsPlanMapper;
import com.xoa.model.equipment.*;
import com.xoa.service.equipment.LimsEquipEventlogService;
import com.xoa.util.LimsJson;
import com.xoa.util.common.StringUtils;
 import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;



/**
 * @作者 杨岩林
 * @创建日期 15:18 2019/12/16
 * @类介绍
 */
@Service
public class LimsEquipEventlogServiceImpl implements LimsEquipEventlogService {

    @Autowired
    private LimsEquipEventlogMapper limsEquipEventlogMapper;
    @Autowired
    private LimsEquipEventsCapyMapper limsEquipEventsCapyMapper;
    @Autowired
    private EquipTypeMapper equipTypeMapper;
    @Autowired
    private LimsEquipEventsPlanMapper limsEquipEventsPlanMapper;
    @Override
    public LimsJson add(LimsEquipEventlog limsEquipEventlog,String json) {
        LimsJson limsJson=new LimsJson(1,"err");
        int a=0;
        int b=0;
        String x=limsEquipEventlog.getFrequency();
        Date exeTime=limsEquipEventlog.getExeTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(exeTime.getTime());
        int currenYear = calendar.get(Calendar.YEAR);
        int currenMonth = calendar.get(Calendar.MONTH);
        int currenDay = calendar.get(Calendar.DAY_OF_MONTH);
        if(x!=null){
        switch (x){
            case "1":
                calendar.set(Calendar.YEAR, currenYear + 2);
                java.sql.Date newDate = new java.sql.Date(calendar.getTimeInMillis());
                limsEquipEventlog.setExpireDate(newDate);
                break;
            case "2":
                calendar.set(Calendar.YEAR, currenYear + 1);
                java.sql.Date newDate1 = new java.sql.Date(calendar.getTimeInMillis());
                limsEquipEventlog.setExpireDate(newDate1);
                break;
            case "3":
                calendar.set(Calendar.MONTH, currenMonth + 1);
                java.sql.Date newDate2 = new java.sql.Date(calendar.getTimeInMillis());
                limsEquipEventlog.setExpireDate(newDate2);
                break;
            case "4":
                calendar.set(Calendar.MONTH, currenMonth + 3);
                java.sql.Date newDate3 = new java.sql.Date(calendar.getTimeInMillis());
                limsEquipEventlog.setExpireDate(newDate3);
                break;
            case "5":
                calendar.set(Calendar.DAY_OF_MONTH, currenDay + 7);
                java.sql.Date newDate4 = new java.sql.Date(calendar.getTimeInMillis());
                limsEquipEventlog.setExpireDate(newDate4);
                break;
            case "6":
                calendar.set(Calendar.DAY_OF_MONTH, currenDay + 1);
                java.sql.Date newDate5 = new java.sql.Date(calendar.getTimeInMillis());
                limsEquipEventlog.setExpireDate(newDate5);
            default:
                break;

        }
        }
        int result =limsEquipEventlogMapper.insertSelective(limsEquipEventlog);
        if(result>0){
            if(!StringUtils.checkNull(json)){
                List<LimsEquipEventsCapy> limsEquipEventsCapyList= JSONArray.parseArray(json,LimsEquipEventsCapy.class);            //List<LimsEquipEventsCapy> limsEquipEventsCapyList = (List<LimsEquipEventsCapy>) JSONArray.toCollection(jsonarray, LimsEquipEventsCapy.class);
            for(LimsEquipEventsCapy limsEquipEventsCapy1:limsEquipEventsCapyList){
                a++;
                limsEquipEventsCapy1.setEventLogId(limsEquipEventlog.getEventLogId());
                int result1= limsEquipEventsCapyMapper.insertSelective(limsEquipEventsCapy1);
                if(result1>0){
                    b++;
                }
            }
            }
            if(a==b){
                LimsEquipEventsPlan limsEquipEventsPlan=limsEquipEventsPlanMapper.selectByPrimaryKey(limsEquipEventlog.getEventId());
                limsEquipEventsPlan.setLastExeTime(limsEquipEventlog.getExeTime());
                limsEquipEventsPlan.setNestExeTime(limsEquipEventlog.getExpireDate());
                int result2=limsEquipEventsPlanMapper.updateByPrimaryKeySelective(limsEquipEventsPlan);
                if(result2>0){
                    limsJson.setCode(0);
                    limsJson.setMsg("执行成功");
                }

            }
        }

        return limsJson;
    }

    @Override
    public LimsJson queryByEventId(Integer eventId,Integer page,Integer limit) {
        LimsJson limsJson=new LimsJson(1,"err");
        Map map=new HashMap();
        PageParams pageParams=new PageParams();
        pageParams.setPageSize(limit);
        pageParams.setPage(page);
        map.put("page",pageParams);
        map.put("eventId",eventId);
        List<LimsEquipEventlog> limsEquipEventlogs=limsEquipEventlogMapper.selectAll(map);
        if(limsEquipEventlogs!=null && limsEquipEventlogs.size()>0){
            limsJson.setCode(0);
            limsJson.setMsg("查询成功");
            limsJson.setData(limsEquipEventlogs);
            limsJson.setCount(pageParams.getTotal());
        }
        return limsJson;
    }

    @Override
    public LimsJson showAll(LimsEquipEventlog limsEquipEventlog, Integer page, Integer limit) {
        LimsJson limsJson=new LimsJson(1,"err");
        Map map=new HashMap();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("LimsEquipEventlog",limsEquipEventlog);
        List<EquipType> equipTypeList = equipTypeMapper.selectChildEquipType(limsEquipEventlog.getEquipTypeId());
        List<Integer> list=new ArrayList<>();
        if(limsEquipEventlog.getEquipTypeId()!=null){
        list.add(limsEquipEventlog.getEquipTypeId());
        if(equipTypeList!=null && equipTypeList.size()>0) {
            for (EquipType equipType : equipTypeList) {
                list.add(equipType.getId());
            }
        }
        map.put("list",list);
        }
        List<LimsEquipEventlog> limsEquipEventlogList=limsEquipEventlogMapper.queryAll(map);
        map.remove("page");
        List<LimsEquipEventlog> limsEquipEventlogList1=limsEquipEventlogMapper.queryAll(map);

        if(limsEquipEventlogList!=null && limsEquipEventlogList.size()>0){
            limsJson.setMsg("查询成功");
            limsJson.setData(limsEquipEventlogList);
            limsJson.setCode(0);
            limsJson.setCount(limsEquipEventlogList1.size());
        }else{
            limsJson.setMsg("无数据");
            limsJson.setCode(0);

        }
        return limsJson;
    }
}
